import { Injectable } from '@nestjs/common';
import { CreateConnectMysqlDto } from './dto/create-connect-mysql.dto';
import { UpdateConnectMysqlDto } from './dto/update-connect-mysql.dto';

// 操作数据库
import { ConnectMysql } from './entities/connect-mysql.entity';
import { InjectRepository } from '@nestjs/typeorm';
// 引入类型 Repository 接受实体泛型
import { Repository, Like } from 'typeorm';

@Injectable()
export class ConnectMysqlService {
  // 依赖注入
  constructor(
    @InjectRepository(ConnectMysql)
    private readonly connectMysql: Repository<ConnectMysql>,
  ) {}
  // 查
  async detect(query: { keyword: string; page: number; pageSize: number }) {
    const data = this.connectMysql.find({
      where: {
        name: Like(`%${query.keyword}%`), // 模糊查询: % 任意数量的字符
      },
      order: {
        id: 'DESC',
      },
      skip: (query.page - 1) * query.pageSize, // 页码
      take: query.pageSize, // 每页大小
    });
    return data;
  }
  // 增
  create(data) {
    const addData = new ConnectMysql();
    addData.name = data.name;
    addData.age = data.age;
    return this.connectMysql.save(data);
  }
  // 改
  update(id: number, updateConnectMysqlDto: UpdateConnectMysqlDto) {
    return this.connectMysql.update(id, updateConnectMysqlDto);
  }
  // 删
  remove(id: number) {
    return this.connectMysql.delete(id);
  }
}
